import { reqSpaceManager } from "@/api/api";
import { jsonp } from 'vue-jsonp' //使用jsonp

export default {
    data() {
        return {
            parkList: [],
            projectList: [], //单位数据
            buildings: [],
            loadingReset: false,
            loadingSearch1: false,
            sysInstanceIndex: 1,
            sysInstanceTotalPages: 10,
            flag: true,
            sysInstanceIndex2: 1,
            sysInstanceTotalPages2: 10,
            sysInstanceIndex3: 1,
            sysInstanceTotalPages3: 10,
            loading: false,
            loading2: false,
            loading3: false,

            getAddressflag: false, //地图选点
            orgName: '',
            buildingsName: '',
            floorName: '',
            selectedValue: "",
            buildingsName2: "",
            floorName2: "",
            hasSearch: false,
            showPopover: false,
        };
    },
    mounted() {},
    methods: {
        //安装点初始化
        clearinit() {
            this.searchForm = {
                parentId: "",
                spaceId: "",
            };
            this.parentId = ""
            this.search_val = "";
            this.page = 1;
            this.limit = 10;
            this.tableData = [];
            this.total = 0;

            this.flag = true;
            this.buildingsName = '';
            this.buildingsName2 = '';
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;
            this.buildings = []

            this.floorName = '';
            this.floorName2 = '';
            this.sysInstanceIndex3 = 1;
            this.sysInstanceTotalPages3 = 10;
            this.floor = []
            this.clearSelections() // 清空复选框√√
        },

        // 下拉请求列表
        buildingsChange(val) {
            if (val && this.flag) {
                this.sysInstanceIndex2 = 1
                this.sysInstanceTotalPages2 = 10
                this.buildings = []
                this.getBuildings(); // 建筑物列表
                this.flag = false; //只执行一次
            }
        },

        // 清空
        clearBuildings() {
            this.buildingsName = '';
            this.buildingsName2 = '';
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;
            this.buildings = []
            this.flag = true;
        },
        // 远程搜索
        async remoteMethod2(query) {
            this.buildingsName = query;
            this.buildingsName2 = query;
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;
            this.buildings = []
            this.getBuildings()
        },
        // 建筑物操作
        async loadMore2() {
            if (this.sysInstanceIndex2 >= Math.ceil(this.sysInstanceTotalPages2 / 10)) return
            this.sysInstanceIndex2++;
            this.loading2 = true;
            // 获取列表数据的方法
            await this.getBuildings()
            this.loading2 = false;
        },
        // 建筑物列表
        async getBuildings() {
            let res = await reqSpaceManager(this.sysInstanceIndex2, 10, {
                userId: this.user.userId,
                roleName: this.user.roleName,
                metaCode: 1,
                orgId: this.orgInfo.orgId,
                isStatistics: 0,
                name: this.buildingsName2
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPages2 = res.data.total
            this.buildings = [...this.buildings, ...res.data.rows]
        },
        // 根据建筑物查询楼层
        projectchange2(e) {
            this.searchForm.parentId = e
            this.searchForm.spaceId = ''
            this.handleClear3()
                // if (e != '') {
                //     this.SpaceManager();
                // }
                // this.searchClick1();
        },
        // 建筑物操作结束

        // 清空
        handleClear3() {
            this.floorName = '';
            this.floorName2 = '';
            this.sysInstanceIndex3 = 1;
            this.sysInstanceTotalPages3 = 10;
            this.floor = []
            if (this.searchForm.parentId != '') {
                this.SpaceManager()
            }
        },
        // 远程搜索
        async remoteMethod3(query) {
            this.floorName = query;
            this.floorName2 = query;
            this.sysInstanceIndex3 = 1;
            this.sysInstanceTotalPages3 = 10;
            this.floor = []
            this.SpaceManager()
        },
        projectchange3(e) {
            this.searchForm.spaceId = e;
            // this.searchClick1();
        },
        // 楼层操作
        async loadMore3() {
            if (this.sysInstanceIndex3 >= Math.ceil(this.sysInstanceTotalPages3 / 10)) return
            this.sysInstanceIndex3++;
            this.loading3 = true;
            // 获取列表数据的方法
            await this.SpaceManager()
            this.loading3 = false;
        },
        // 楼层列表
        async SpaceManager() {
            let res = await reqSpaceManager(this.sysInstanceIndex3, 10, {
                userId: this.user.userId,
                roleName: this.user.roleName,
                metaCode: 2,
                orgId: this.orgId,
                parentId: this.searchForm.parentId,
                isStatistics: 0,
                name: this.floorName2
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPages3 = res.data.total
            this.floor = [...this.floor, ...res.data.rows]
        },

        // // 搜索
        // async searchClick1() {
        //     this.page = 1;
        //     this.limit = 10;
        //     this.loadingSearch1 = true;
        //     // 如果只选了建筑物就是建筑物的id,选了楼层就是传楼层的id
        //     if (this.searchForm.spaceId != null && this.searchForm.spaceId != '') {
        //         this.parentId = this.searchForm.spaceId;
        //     } else {
        //         this.parentId = this.searchForm.parentId;
        //     }
        //     await this.getreqParkList();
        //     this.loadingSearch1 = false;
        //     this.clearSelections(); //清空复选框
        // },

        // 搜索
        async onSearch() {
            this.showPopover = false;
            this.page = 1;
            this.limit = 10;
            this.loadingSearch1 = true;
            // 如果只选了建筑物就是建筑物的id,选了楼层就是传楼层的id
            if (this.searchForm.spaceId != null && this.searchForm.spaceId != '') {
                this.parentId = this.searchForm.spaceId;
            } else {
                this.parentId = this.searchForm.parentId;
            }
            await this.getreqParkList();
            this.loadingSearch1 = false;
            this.clearSelections(); //清空复选框
        },
        // 高级搜索重置
        async resetClick() {
            this.parentId = ''
            this.buildingsName = ''
            this.floorName = ''
            this.searchForm = {
                parentId: "",
                spaceId: "", //楼层
                isDefence: ""
            };
            this.floor = []
            this.page = 1;
            this.limit = 10;
            this.loadingReset = true;
            await this.getreqParkList();
            this.loadingReset = false;
        },
        // 导入
        importClick() {
            this.$refs.refImportDevice.getProductList();
            this.$refs.refImportDevice.getManufacturerList();
            this.$refs.refImportDevice.RegionList();
            this.$refs.refImportDevice.closeAddDialog2(); // 防止弹出验证
            this.$refs.refImportDevice.dialogVisible = true;
        },

        // 监听获取经纬度
        addListenAddress() {
            window.addEventListener(
                "message",
                (event) => {
                    // console.log(event);
                    // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
                    let loc = event.data;
                    // console.log(loc);
                    // console.log(loc.module);
                    if (loc && loc.module == "locationPicker") {
                        //防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
                        // console.log(loc, "loc");
                        this.getAddress(loc.latlng.lat, loc.latlng.lng, loc.poiname);
                        // this.isShowMap = false
                    }
                },
                false
            );
        },
        // 获取地址信息
        getAddress(lat, lng, poiname) {
            jsonp(
                    `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&key=44FBZ-OVKKP-T23D5-VGHUE-XDXC3-BDF53`, {
                        output: "jsonp",
                    }
                )
                .then((res) => {
                    // console.log(res);
                    this.getAddressflag = !this.getAddressflag;
                    let { province, city, district } = res.result.ad_info;
                    let { title } = res.result.address_reference.town;
                    // console.log(res.result, "res.result.ad_info");
                    this.$set(this.getLocationInfo, "getLat", lat);
                    this.$set(this.getLocationInfo, "getLng", lng);
                    this.$set(this.getLocationInfo, "province", province);
                    this.$set(this.getLocationInfo, "city", city);
                    this.$set(this.getLocationInfo, "district", district);
                    this.$set(this.getLocationInfo, "street", title);
                    // 详细位置
                    this.$set(
                        this.getLocationInfo,
                        "getAdd",
                        this.stripscript(poiname).length > 20 ?
                        this.stripscript(poiname).substr(0, 20) :
                        this.stripscript(poiname)
                    );
                })
                .catch((err) => {
                    // console.log(err, "err");
                });
        },
        // 过滤特殊字符
        stripscript(s) {
            let pattern = new RegExp(
                "[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）&mdash;—|{}【】‘；：”“'。，、？]"
            );
            let rs = "";
            for (let i = 0; i < s.length; i++) {
                rs = rs + s.substr(i, 1).replace(pattern, "");
            }
            return rs;
        },

    },
};